﻿Imports System.IO
Public Class ClsFile
    'represents general file

#Region "Attributes"
#Region "Private attributes"
    Protected MeStateOfFile As GeFileState = GeFileState.kkNotInit  'state of file
    Protected MsPath As String = "" 'path to file with filename
#End Region
#End Region
#Region "Constructors"
    Sub New()

    End Sub
    Sub New(ByVal IsPath As String, Optional ByVal IbCreateFile As Boolean = False)
        Init(IsPath, IbCreateFile)
    End Sub
#End Region
#Region "Properties"
#Region "Public attributes"
    Public ReadOnly Property StateOfFile() As GeFileState
        Get
            Return MeStateOfFile
        End Get
    End Property
    Public ReadOnly Property Path() As String
        Get
            Return MsPath
        End Get
    End Property
#End Region
#End Region
#Region "Functions"
    'initialization of file
    Public Overridable Function Init(ByVal IsPath As String, Optional ByVal IbCreateFile As Boolean = False) As GeActionResult
        Try
            Dim LeResult As GeActionResult = GeActionResult.kkError

            If MeStateOfFile = GeFileState.kkNotInit Then
                MsPath = IsPath 'set path in all cases! (path exists or no)

                If Not IO.File.Exists(IsPath) Then
                    If IbCreateFile = True Then
                        If CreateFile(IsPath) = GeActionResult.kkOk Then
                            LeResult = GeActionResult.kkOk
                        Else
                            Throw New Exception
                        End If
                    Else
                        Throw New Exception("Subor " & IsPath & " neexistuje")
                    End If
                Else
                    LeResult = GeActionResult.kkOk
                End If
            Else
                Throw New Exception
            End If

            MeStateOfFile = GeFileState.kkReadyForOpen
            Return LeResult
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'opens file
    Public Overridable Function OpenFile() As GeActionResult
        Try
            If MeStateOfFile = GeFileState.kkReadyForOpen Then
                Return GeActionResult.kkOk
            Else
                Debug.Print("CHYBA!!! - ClsFile.OpenFile")
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'closes file
    Public Overridable Function CloseFile() As GeActionResult
        Try
            If (MeStateOfFile = GeFileState.kkNewFile) Or (MeStateOfFile = GeFileState.kkOpened) Then
                Return GeActionResult.kkOk
            Else
                Debug.Print("CHYBA!!! - ClsFile.CloseFile")
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'set new file path - e.g. after renaming a project file, modul file etc
    Protected Function SetNewFilePath(ByVal IsNewFilePath As String) As GeActionResult
        Try
            MsPath = IsNewFilePath
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region

End Class
